package com.bw.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.exam.pojo.Specification;
import com.bw.exam.pojo.response.R;
import com.bw.exam.service.SpecificationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2024/7/23 22:15
 **/
@RestController
@RequestMapping("/platform/specification")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    @RequestMapping("list/{startPage}/{endPage}")
    public R list(@RequestParam("specName") String specName, @PathVariable int startPage, @PathVariable int endPage){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(specName)){
            queryWrapper.like("spec_name",specName);
        }

        Page<Specification> page = new Page<Specification>(startPage,endPage);
        Page<Specification> responsePage = this.specificationService.page(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("list",responsePage.getRecords());
        map.put("total",responsePage.getTotal());
        return R.success(map);
    }


    @RequestMapping("listAll")
    public R listAll(){
        List<Specification> list = this.specificationService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        return R.success(map);
    }




    public  R add(@RequestBody Specification specification){
        return R.success(this.specificationService.insert(specification));
    }


}
